package com.mdp.im.core.publish.action.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mdp.core.api.Sequence;
import com.mdp.core.err.BizException;
import com.mdp.im.core.entity.ImGroup;
import com.mdp.im.core.entity.ImGroupUser;
import com.mdp.im.core.publish.action.AbstractPublishAction;
import com.mdp.im.core.publish.api.action.PublishAction;
import com.mdp.im.core.service.admin.GroupAdminService;
import com.mdp.im.core.publish.api.Publisher;
import com.mdp.im.core.service.ImGroupService;
import com.mdp.im.core.service.ImGroupUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 加入群组消息处理器
 * 
 * @author chenyc
 * @date 2018/5/28
 */
@Service("pushJoinChannelGroupAction")
public class JoinChannelGroupAction extends AbstractPublishAction implements PublishAction {

	@Autowired
    Sequence seqService;
	@Autowired
	ImGroupService groupService;
	@Autowired
	ImGroupUserService groupUserService;
	
	@Autowired
	Publisher publisher;

    @Autowired
	GroupAdminService groupAdminService;
	
    /**
     * 加入群
     * 1.发送群通知
     * 2.将人员写入数据库 group_user表 
     * 
     * @param message
     * message:{ 
     *  action:'joinChannelGroup',
     *  msgType:'group',  
     *  sendUserid:'发送者编号',
     *  sendUsername:'发送者姓名', 
     * 	groupId:'群组编号不能为空',
     *  groupName:'群组名称不能为空',
     *  users:{
     *  	userid:'成员编号',
     *      username:'成员姓名',
     *      headimgurl:'成员头像'
     *  }
     * }
     * 
     * 
     */
    @Override
    public void doAction(JSONObject message) {
    	JSONObject json = message;
        if (!json.containsKey("users")) {
            return;
        } 
        String groupId=json.getString("groupId"); 
          JSONArray jsonUsers=json.getJSONArray("users");
        List<ImGroupUser> groupUsers=new ArrayList<>(); 
        if(groupAdminService.isForbiddenJoin(groupId)) {
        	return;
        }
        ImGroup groupDb=groupService.getImGroupByGroupId(groupId);
        if(groupDb==null) {
        	throw new BizException("群不存在");
        }
        if("0".equals(groupDb.getAllowJoin())) {
        	throw new BizException("群不允许加人");
        }
        message.put("groupName", groupDb.getGroupName()); 
        message.put("categoryId", groupDb.getCategoryId());
        message.put("cuserid", groupDb.getCuserid());
        message.put("cusername", groupDb.getCusername());
        message.put("allowJoin", groupDb.getAllowJoin());
        message.put("allowSpeechTime", groupDb.getAllowSpeechTime()); 
        message.put("gstatus", groupDb.getGstatus());

        for (Object user : jsonUsers) {
        	JSONObject jsonUser=(JSONObject)user;
        	String userid=jsonUser.getString("userid");
        	String username=jsonUser.getString("username");
        	String headimgurl=jsonUser.getString("headimgurl");
        	ImGroupUser groupUser=new ImGroupUser(); 
        	groupUser.setStatus("1"); 
        	groupUser.setUserid(userid);
        	groupUser.setUsername(username);
        	groupUser.setGroupId(groupId);
        	groupUser.setHeadimgurl(headimgurl); 
        	groupUser.setInviteTime(new Date()); 
        	groupUsers.add(groupUser); 
        } 
        
        publisher.publishGroupMessage(groupId, message.toJSONString());
        this.groupService.asyncBatchJoinGroupUsers(groupDb,groupUsers);
        
          
    }

}
